#include <iostream>
#include <cstdio>
#include <cmath>
#define hidden 30
#define output 225
#define feature 225

using namespace std;

double v[hidden][feature];
double w[output][hidden];
double v_b[hidden];
double w_b[output];
double putin[225] = {0};
double _out[30] = {0};
double t_out[225] = {0};

void predict()
{
    for (int i = 0; i < hidden; i++)
    {
        _out[i] = 0;
        for (int j = 0; j < feature; j++)
        {
            _out[i] += putin[j] * v[i][j];
        }
        _out[i] += v_b[i];
        _out[i] = (double)1 / (double(1 + exp(-_out[i])));
    }
    for (int i = 0; i < output; i++)
    {
        t_out[i] = 0;
        for (int j = 0; j < hidden; j++)
        {
            t_out[i] += _out[j] * w[i][j];
        }
        t_out[i] += w_b[i];
        t_out[i] = (double)1 / (double(1 + exp(-t_out[i])));
    }
    int wei_x = 0, wei_y = 0, maxii = 0;
    double maxi = 0;
    for (int i = 0; i < output; i++)
    {

        if (t_out[i] > maxi)
        {
            maxii = i;
            maxi = t_out[i];
            wei_x = i / 15;
            wei_y = i - wei_x * 15;
        }
        printf("%d: %lf\n", i, t_out[i]);
    }
    printf("i: %d x: %d  y: %d ", maxii, wei_x, wei_y);
}

void paraUpload()
{
    FILE *wp = fopen("nettest/ww.txt", "r");
    FILE *vp = fopen("nettest/vv.txt", "r");
    FILE *vb = fopen("nettest/v_b.txt", "r");
    FILE *wb = fopen("nettest/w_b.txt", "r");
    if (wp == NULL || vp == NULL || vb == NULL || wb == NULL)
    {
        cout << "files open false";
        exit(0);
    }
    double temp;
    int i = 0, j = 0;
    while (fscanf(vp, "%lf", &temp) != EOF)
    {
        v[i][j] = temp;
        j++;
        if (j == 225)
        {
            j = 0;
            i++;
        }
    }
    i = 0;
    j = 0;
    while (fscanf(wp, "%lf", &temp) != EOF)
    {
        w[i][j] = temp;
        j++;
        if (j == 30)
        {
            j = 0;
            i++;
        }
    }
    i = 0;
    while (fscanf(vb, "%lf", &temp) != EOF)
    {
        v_b[i++] = temp;
    }
    i = 0;
    while (fscanf(wb, "%lf", &temp) != EOF)
    {
        w_b[i++] = temp;
    }
    fclose(wp);
    fclose(vp);
    fclose(vb);
    fclose(wb);
}

int main()
{
    putin[112] = 1;
    putin[206] = -1;
    paraUpload();
    predict();
    system("pause"); //206: 0.611299
    return 0;
}
